import os

import dotenv
from langchain_openai import ChatOpenAI
from langgraph.prebuilt import create_react_agent

dotenv.load_dotenv()


def get_weather_tool(city: str) -> str:
    """获取指定城市的天气"""
    return f"{city}今天天气晴朗,温度25°"


# 1 构建LLM
llm = ChatOpenAI(
    model="glm-4",
    base_url=os.getenv("ZP_BASE"),
    api_key=os.getenv("ZP_API_KEY"),
    temperature=0,
).bind_tools([get_weather_tool])  # 需要给llm绑定上tool

# 2 构建agent
agent = create_react_agent(
    model=llm,
    tools=[get_weather_tool],  # 添加到agent
    prompt="你是一个智能助手"
)

print(agent.invoke({"messages": [{"role": "user", "content": "西安今天的天气怎么样?"}]}))
